package com.hoperun.checkcloth.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ListView;

import com.blankj.utilcode.util.ResourceUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hoperun.checkcloth.R;
import com.hoperun.checkcloth.adapter.SJZDAdapter;
import com.hoperun.checkcloth.adapter.XcslAdapter;
import com.hoperun.checkcloth.base.MIP_BaseActivity;
import com.hoperun.checkcloth.entity.ClothEntity;
import com.hoperun.checkcloth.entity.SJZDEntity;
import com.hoperun.checkcloth.entity.XCXX;
import com.hoperun.checkcloth.fragment.CountDialogFragment;
import com.hoperun.checkcloth.net.IServiceImpl;
import com.hoperun.checkcloth.net.RequestServiceImpl;
import com.hoperun.checkcloth.util.SharedPreUtil;
import com.hoperun.checkcloth.view.SimpleDialog;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class PfgclMainActivity extends MIP_BaseActivity implements OnClickListener {

    /**
     * 查询词典类型的数据字典id
     **/
    private static final String SJZDID_TASKLX = "BADLX";

    private EditText monadNoEt;
    private EditText monadNo;

    private EditText clothNoEt;
    private EditText scanTime;
    private EditText weightEt;
    private EditText gh;
    private Button backBtn;

    /**
     * 三联单号是否正确
     **/
    private boolean isMonadNoCorrect = false;

    /**
     * 匹数
     **/
    private int mPsSum = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pfgcl_activity_main);
        initViews();
        initEvents();

    }

    private void initViews() {

        monadNoEt = (EditText) findViewById(R.id.monadNoEt);
        monadNo = (EditText) findViewById(R.id.monadNo);
        clothNoEt = (EditText) findViewById(R.id.clothNoEt);
        weightEt = (EditText) findViewById(R.id.weightEt);
        scanTime = (EditText) findViewById(R.id.scanTime);
        gh = (EditText) findViewById(R.id.gh);
        backBtn = (Button) findViewById(R.id.backBtn);


        String empID = (String) SharedPreUtil.getValue(this,
                SharedPreUtil.YHBH, "");
        gh.setText(empID);
    }

    private void initEvents() {

        backBtn.setOnClickListener(this);

        monadNoEt.setOnKeyListener((v, keyCode, event) -> {
            if (event != null
                    && event.getKeyCode() == KeyEvent.KEYCODE_ENTER
                    && event.getAction() == KeyEvent.ACTION_UP) {

                String monadNo = monadNoEt.getText().toString().trim();
                if (monadNo.length() >= 4) {
                    // 查询信息
                    cllrByMonadNo(monadNo);
                } else {
                    SimpleDialog
                            .show(PfgclMainActivity.this, getResources().getString(R.string.monadno_not_right));
                }
                return true;
            }
            return false;
        });


        monadNoEt.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                isMonadNoCorrect = false;// 三联号改变了需要重新校验
//                clothNoEt.setText("");
//                weightEt.setText("");
//                scanTime.setText("");

            }
        });

    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.backBtn:
                this.finish();
                break;
            default:
                break;
        }

    }


    /**
     * 根据三联号获取相关基本信息
     *
     * @param monadNo
     */
    private void cllrByMonadNo(String monadNo) {
        showWait();
        String YHBH = (String) SharedPreUtil.getValue(this, SharedPreUtil.YHBH,
                "");
        RequestServiceImpl.cllrByMonadNo(mMIPHandler,
                IServiceImpl.TYPE_zjQueryByMonadNo, monadNo, YHBH);
    }



    @Override
    public void onPostHandle(int requestType, Object objBody, boolean error)
            throws Exception {
        stopWait();
        String msg = "";
        String opt = "";
        if (error) {
            switch (requestType) {
                case IServiceImpl.TYPE_zjQueryByMonadNo:
                    try {
                        JSONObject jsonObj = new JSONObject((String) objBody);
                        msg = jsonObj.optString("ERRORMESSAGE");
                        opt = jsonObj.optString("FLAG");


                        if ("SUCCESS".equals(opt)) {
                            JSONObject data = jsonObj.optJSONObject("DATA");
                            //2019-6-23新增弹出提示
                            String levelFlag = jsonObj.optString("LEVELFLAG");
                            if (!TextUtils.isEmpty(levelFlag)) {
                                SimpleDialog.show(this, levelFlag);
                            }

                            if (data != null) {
                                SimpleDialog.show(this, getResources().getString(R.string.insert_success));
                                monadNo.setText(monadNoEt.getText().toString());
                                isMonadNoCorrect = true;// 查询成功
//                                clothNoEt.setText(data.getString("CLOTHTKTNO"));
                                String a = data.getInt("FINISHWEIGHT")+"";
                                clothNoEt.setText(data.getString("BH"));
                                weightEt.setText(data.getInt("FINISHWEIGHT")+"");
                                scanTime.setText(data.optString("SCANTIME"));

                            }

                        } else {
                            if (TextUtils.isEmpty(msg)) {
                                SimpleDialog.show(this, getResources().getString(R.string.insert_fail));
                            } else {
                                SimpleDialog.show(this, msg);
                            }

                        }
                        monadNoEt.setText("");
                    } catch (Exception e) {
                        monadNoEt.setText("");
                        e.printStackTrace();
                        SimpleDialog.show(this, getResources().getString(R.string.insert_fail));
                    }
                    break;
            }
        } else {
            if (objBody != null) {
                String obj = objBody.toString();
                SimpleDialog.show(this, obj);
            }

        }

    }

}
